/** 
* @file         input.c
* @brief        This is a brief description. 
* @details  This is the detail description. 
* @author     Administrator
* @date     Jun 10, 2014
* @version  A001 
* @par Copyright (c):  
*       
* @par History:          
*   version: Administrator, Jun 10, 2014\n 
*/

#include "input.h"
#include "../bsp/bsp.h"
#include "../lib/delay.h"
#include "uart.h"
#include "io_configure.h"
#ifdef __STM32
    #include "../bsp/arch/stm32/stm32f1xx/STM32F10x_StdPeriph_Driver/inc/stm32f10x_gpio.h"
#endif
input_status_process status_process;
uint8 input_available_counter;
uint32 adjust_input_available;

#define INPUT_READ_PERIOD 1
#define INPUT_INIT_STATUS (0xFFFFFFFF)

/*! \brief 初始化输入端口
 * \param
 * \retrun
 */
void input_init(void)
{
#if INPUT_MAX_NUM > 0
    set_in0_input();
#endif
#if INPUT_MAX_NUM > 1
    set_in1_input();
#endif
#if INPUT_MAX_NUM > 2
    set_in2_input();
#endif
#if INPUT_MAX_NUM > 3
    set_in3_input();
#endif
#if INPUT_MAX_NUM > 4
    set_in4_input();
#endif
#if INPUT_MAX_NUM > 5
    set_in5_input();
#endif
#if INPUT_MAX_NUM > 6
    set_in6_input();
#endif
#if INPUT_MAX_NUM > 7
    set_in7_input();
#endif
#if INPUT_MAX_NUM > 8
    set_in10_input();
#endif
#if INPUT_MAX_NUM > 9
    set_in11_input();
#endif
#if INPUT_MAX_NUM > 10
    set_in12_input();
#endif
#if INPUT_MAX_NUM > 11
    set_in13_input();
#endif
#if INPUT_MAX_NUM > 12
    set_in14_input();
#endif
#if INPUT_MAX_NUM > 13
    set_in15_input();
#endif
#if INPUT_MAX_NUM > 14
    set_in16_input();
#endif
#if INPUT_MAX_NUM > 15
    set_in17_input();
#endif
#if INPUT_MAX_NUM > 16
    set_in20_input();
#endif
#if INPUT_MAX_NUM > 17
    set_in21_input();
#endif
    adjust_input_available = INPUT_OLD = INPUT_CUR = INPUT_ADJUST = INPUT_INIT_STATUS;
    input_available_counter = 0;
    apply4delay(INPUT_DELAY_FLAG, 1);
}
/*! \brief 取得输入端口状态
 * \param
 * \retrun
 */
static void get_input(void)
{
#if INPUT_MAX_NUM > 0
    IN0_adjust = IN0_PIN;
#endif
#if INPUT_MAX_NUM > 1
    IN1_adjust = IN1_PIN;
#endif
#if INPUT_MAX_NUM > 2
    IN2_adjust = IN2_PIN;
#endif
#if INPUT_MAX_NUM > 3
    IN3_adjust = IN3_PIN;
#endif
#if INPUT_MAX_NUM > 4
    IN4_adjust = IN4_PIN;
#endif
#if INPUT_MAX_NUM > 5
    IN5_adjust = IN5_PIN;
#endif
#if INPUT_MAX_NUM > 6
    IN6_adjust = IN6_PIN;
#endif
#if INPUT_MAX_NUM > 7
    IN7_adjust = IN7_PIN;
#endif
#if INPUT_MAX_NUM > 8
    IN10_adjust = IN10_PIN;
#endif
#if INPUT_MAX_NUM > 9
    IN11_adjust = IN11_PIN;
#endif
#if INPUT_MAX_NUM > 10
    IN12_adjust = IN12_PIN;
#endif
#if INPUT_MAX_NUM > 11
    IN13_adjust = IN13_PIN;
#endif
#if INPUT_MAX_NUM > 12
    IN14_adjust = IN14_PIN;
#endif
#if INPUT_MAX_NUM > 13
    IN15_adjust = IN15_PIN;
#endif
#if INPUT_MAX_NUM > 14
    IN16_adjust = IN16_PIN;
#endif
#if INPUT_MAX_NUM > 15
    IN17_adjust = IN17_PIN;
#endif
#if INPUT_MAX_NUM > 16
    IN20_adjust = IN20_PIN;
#endif
#if INPUT_MAX_NUM > 17
    IN21_adjust = IN21_PIN;
#endif
}
/*! \brief 输入端口的状态机 
 *      用于消抖跟输入处理 
 * \param
 * \retrun
 */
void input_status_mechine(void)
{
    if (get_delay_flag(INPUT_DELAY_FLAG))
    {
        get_input();
        if ((adjust_input_available & INPUT_ADJUST) == adjust_input_available)
        {
            input_available_counter++;
        } else
        {
            adjust_input_available = INPUT_ADJUST;
            input_available_counter = 0;
        }
        if (input_available_counter > 4)
        {
            INPUT_OLD = INPUT_CUR;
            INPUT_CUR = INPUT_ADJUST;
            input_available_counter = 0;
        }
        apply4delay(INPUT_DELAY_FLAG, INPUT_READ_PERIOD);
    }
}
